/* 
 * File:   ESGame.cpp
 * Author: Semidán Acosta Betancor
 * 
 * Created on 17 de septiembre de 2011, 22:41
 */

#include "ESGame.h"
#include "ESGameState.h"

#include <SFML/Graphics.hpp>

namespace ee
{

ESGame::ESGame(int width, int height, std::string &title) : running(false)
{
    renderWindow.Create(sf::VideoMode(width, height, 32), title);
}

ESGame::~ESGame()
{
}

void ESGame::handleEvents()
{
    ESGameState *gameState = gameStates.back();
    if(gameState != NULL)
    {
        gameState->handleEvents(renderWindow);
    }
}

void ESGame::update()
{
    ESGameState *gameState = gameStates.back();
    if (gameState != NULL)
    {
        gameState->update(renderWindow);
    }
}

void ESGame::draw()
{
    ESGameState *gameState = gameStates.back();
    if (gameState != NULL)
    {
        gameState->draw(renderWindow);
    }
}

void ESGame::display()
{ 
    renderWindow.Display();
}

void ESGame::init()
{
    running=true;
}

void ESGame::cleanup()
{
    cleanGameStates();
    renderWindow.Close();
}

void ESGame::addGameState(ESGameState* gameState)
{
    if (!gameStates.empty())
    {
        gameStates.back()->pause();
    }

    gameStates.push_back(gameState);
}

void ESGame::changeState(ESGameState* gameState)
{
}

void ESGame::cleanGameStates()
{
    while (!gameStates.empty())
    {

        ESGameState *gameState = gameStates.back();
        if (gameState != NULL)
        {
            gameState->pause();
            gameState->cleanup();
        }

        gameStates.pop_back();
        delete gameState;
    }
}

}